﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DBUtility;
namespace DAL
{
    public class CardMaster
    {
        public CardMaster()
        { }

        public bool CardMasterAdd(MODEL.CardMaster model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@carddeputyid",SqlDbType.VarChar,20),
                                       new SqlParameter("@cardnum",SqlDbType.VarChar,50),
                                       new SqlParameter("@createtime",SqlDbType.DateTime),
                                       new SqlParameter("@expiretime",SqlDbType.DateTime),
                                       new SqlParameter("@usetime",SqlDbType.VarChar,12),
                                       new SqlParameter("@isuse",SqlDbType.Char,1),
                                       new SqlParameter("@isexpire",SqlDbType.Char,1),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = GetID();
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.CCARDDEPUTYID;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.CCARDNUM;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.CCREATETIME;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.CEXPIRETIME;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.CUSETIME;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.CISUSE;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.CISEXPIRE;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProCardmasterAdd", parameter);
        }
        public bool CardMasterUpdate()
        {
            SqlParameter[] parameter = { new SqlParameter("@Return", SqlDbType.Int) };
            parameter[0].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProCradmasterUpdateExpire", parameter);
        }
        public bool CardMasterUpdate(string cId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = cId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProCardmasterUpdateUse", parameter);
        }
        public DataSet CardMasterList(string cId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20) };
            parameter[0].Value = cId;
            parameter[0].Direction = ParameterDirection.Input;

            return DbHelper.RunProcedureS("ProCardmasterSelect", parameter);
        }
        public DataSet CardMasterList(string cId, int nUm)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@num",SqlDbType.Int)};
            parameter[0].Value = cId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = nUm;
            parameter[1].Direction = ParameterDirection.Input;

            return DbHelper.RunProcedureS("ProCarmasterSelectTop", parameter);
        }
        public string GetID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(CID) FROM G_CARDMASTER WHERE CID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }
    }
    public class CardDeputy
    {
        public CardDeputy()
        { }

        public bool CardDeputyAdd(MODEL.CardDeputy model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@gameid",SqlDbType.VarChar,20),
                                       new SqlParameter("@cardname",SqlDbType.VarChar,100),
                                       new SqlParameter("@cardnameinfo",SqlDbType.Text),
                                       new SqlParameter("@cardtype",SqlDbType.Int),
                                       new SqlParameter("@isuse",SqlDbType.Char,1),
                                       new SqlParameter("@activation",SqlDbType.VarChar,500),
                                       new SqlParameter("@state",SqlDbType.Char,1),
                                       new SqlParameter("@money",SqlDbType.Int),
                                       new SqlParameter("@cardnum",SqlDbType.Int),
                                       new SqlParameter("@show",SqlDbType.Char,1),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = GetID();
            parameter[1].Value = model.CGAMEID;
            parameter[2].Value = model.CCARDNAME;
            parameter[3].Value = model.CCARDNAMEINFO;
            parameter[4].Value = model.CCARDTYEP;
            parameter[5].Value = model.CISUSE;
            parameter[6].Value = model.CACTIVATION;
            parameter[7].Value = model.CSTATE;
            parameter[8].Value = model.CMONEY;
            parameter[9].Value = model.CCARDNUM;
            parameter[10].Value = model.CSHOW;
            for (int i = 0; i < 11; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[11].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProCarddeputyAdd", parameter);
        }
        public bool CardDeputyUpdate(MODEL.CardDeputy model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@gameid",SqlDbType.VarChar,20),
                                       new SqlParameter("@cardname",SqlDbType.VarChar,100),
                                       new SqlParameter("@cardnameinfo",SqlDbType.Text),
                                       new SqlParameter("@cardtype",SqlDbType.Int),
                                       new SqlParameter("@isuse",SqlDbType.Char,1),
                                       new SqlParameter("@activation",SqlDbType.VarChar,500),
                                       new SqlParameter("@state",SqlDbType.Char,1),
                                       new SqlParameter("@money",SqlDbType.Int),
                                       new SqlParameter("@cardnum",SqlDbType.Int),
                                       new SqlParameter("@show",SqlDbType.Char,1),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.CID;
            parameter[1].Value = model.CGAMEID;
            parameter[2].Value = model.CCARDNAME;
            parameter[3].Value = model.CCARDNAMEINFO;
            parameter[4].Value = model.CCARDTYEP;
            parameter[5].Value = model.CISUSE;
            parameter[6].Value = model.CACTIVATION;
            parameter[7].Value = model.CSTATE;
            parameter[8].Value = model.CMONEY;
            parameter[9].Value = model.CCARDNUM;
            parameter[10].Value = model.CSHOW;
            for (int i = 0; i < 11; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[11].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProCarddeputyUpdate", parameter);
        }
        public bool CardDeputyUpdate(MODEL.CardDeputy model, string flagName)
        {
            string parName=string.Empty;
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@flag",SqlDbType.Char,1),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.CID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            switch (flagName)
            {
                case "use":
                    {
                        parameter[1].Value=model.CISUSE;
                        parName="ProCarddeputyUpdateUse";
                        break;
                    }
                case "show":
                    {
                        parameter[1].Value=model.CSHOW;
                        parName="ProCarddeputyUpdateShow";
                        break;
                    }
                default: break;
            }
            return DbHelper.RunProcedure(parName, parameter);
        }
        public DataSet CardDeputyList()
        {
            return DbHelper.RunProcedure("ProCarddeputySelect");
        }
        public DataSet CardDeputyList(string id, string flagName)
        {
            string parName = string.Empty;
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20) };
            parameter[0].Value = id;
            parameter[0].Direction = ParameterDirection.Input;
            switch (flagName)
            {
                case "id":
                    {
                        parName = "ProCarddeputySelectByID";
                        break;
                    }
                case "game":
                    {
                        parName = "ProCarddeputySelectByGameID";
                        break;
                    }
                default: break;
            }
            return DbHelper.RunProcedureS(parName, parameter);
        }
        public DataSet CardDeputyList(int cardType)
        {
            SqlParameter[] parameter = { new SqlParameter("@cardtype", SqlDbType.Int) };
            parameter[0].Value = cardType;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProCarddeputySelectByCardType", parameter);
        }
        public string GetID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(CID) FROM G_CARDDEPUTY WHERE CID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }
    }
}
